import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class RoomStatisticsDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;

    public static async insert(param: object) {
        let room_id: number = param["room_id"]
        let clubid: number = param["clubid"]
        let time: number = param["time"]
        let timeStr: string = DateUtil.formatDateStr(time)
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "insert into t_room_statistics(room_id,clubid,time) values(?,?,?)"
        let sqlParams = [room_id, clubid, timeStr];
        let rows = await client.queryPromise(sql, sqlParams);
    }

    public static async get_statistics(club_id: number, startTime: string, endTime: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql = `select  * from  t_room_statistics where time >= ? and time < ? `
        let sqlParams:Array<any> = [startTime, endTime];
        if (club_id > 0) {
            sql += " and clubid=?"
            sqlParams.push(club_id)
        }
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("get_statistics==", sql, sqlParams, results)
        return results
    }

    public static async clear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_room_statistics where time < ?"
        let sqlParams = [DateUtil.formatDateStr(delete_time)];
        await client.queryPromise(sql, sqlParams);
    }


}